#include<thread>
#include<iostream>
#include<condition_variable>
using namespace std;

int main()
{
    int n = 1;
    mutex mtx;
    bool flag = true;
    condition_variable cond;
    thread t1([&]
        {
            while (n < 100)
            {
                unique_lock<mutex> ul(mtx);
                cond.wait(ul, [&flag] {return flag; });
                flag = !flag;
                cout << this_thread::get_id() << " " << n << endl;
                n++;
                cond.notify_one();
            }
        });

    thread t2([&n, &mtx, &flag, &cond]
        {
            while (n < 100)
            {
                unique_lock<mutex> ul(mtx);
                cond.wait(ul, [&flag] {return !flag; });
                flag = !flag;
                cout << this_thread::get_id() << " " << n << endl;
                n++;
                cond.notify_one();
            }
        });

    t1.join();
    t2.join();
    return 0;
}